<template>
  <el-dialog
    v-if="openDialog"
    :title="title"
    v-model="openDialog"
    width="520px"
    @close="onClose"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRefs" :model="form" label-width="85px" :rules="rules">
      <el-form-item label="过滤字段:" prop="fieldName">
        <el-select
          style="width: 85%"
          v-model="form.fieldName"
          placeholder="请选择"
          @change="fieldNameChange"
        >
          <el-option
            v-for="item in fieldNameList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="过滤条件:" prop="operator">
        <el-select
          style="width: 85%"
          v-model="form.operator"
          placeholder="请选择"
        >
          <el-option
            v-for="item in listData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="过滤值:" prop="fieldValue">
        <el-select
          v-if="showSelect"
          style="width: 85%"
          v-model="form.fieldValue"
          placeholder="请选择"
        >
          <el-option
            v-for="item in selectList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          v-else
          style="width: 85%"
          v-model="form.fieldValue"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="组件别名:" prop="comName">
        <el-input
          style="width: 85%"
          v-model="form.comName"
          placeholder="请输入"
        ></el-input>
        <p class="directions">
          说明：模糊匹配时，可以输入多个过滤值（使用空格符分开），各个过滤值之间是“或者”关系，被过滤字段包含任意一个过滤值即可以认为是满足过滤条件
        </p>
      </el-form-item>
    </el-form>
    <template #footer class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="isOk">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup name="filterModule">
import { watch, toRefs, reactive } from 'vue';
const props = defineProps({
  data: {
    type: Object,
    default: () => {},
  },
  open: {
    type: Boolean,
    default: false,
  },
});
const { data, open } = toRefs(props);

let title = ref('');
let openDialog = ref(false);
let showSelect = ref(false);
const formRefs = ref(null);
let selectList = ref([]);
const listData = ref([
  {
    label: '不等于',
    value: 'NOT_EQUAL',
  },
  {
    label: '等于',
    value: 'EQUAL',
  },
  {
    label: '模糊查询',
    value: 'FUZZY',
  },
  {
    label: '大于',
    value: 'GREATER_THAN',
  },
  {
    label: '小于',
    value: 'SMALL_THAN',
  },
]);
const fieldNameList = ref([
  {
    label: '数源系统应用id',
    value: 'appId',
  },
  {
    label: '事件分级',
    value: 'level',
  },
  {
    label: '事件类型',
    value: 'type',
  },
  {
    label: '事件源id',
    value: 'externalId',
  },
  {
    label: '处置状态',
    value: 'state',
  },
  {
    label: '事件标题',
    value: 'subject',
  },
  {
    label: '发生地址',
    value: 'location',
  },
  {
    label: '经度',
    value: 'longitude',
  },
  {
    label: '维度',
    value: 'latitude',
  },
  {
    label: '区域编码',
    value: 'streetCode',
  },
  {
    label: '所属街镇',
    value: 'street',
  },
  {
    label: '自动校正地址',
    value: 'streetVerified',
  },
  {
    label: '上报人姓名',
    value: 'name',
  },
  {
    label: '上报人电话',
    value: 'mobile',
  },
  {
    label: '事件内容',
    value: 'content',
  },
  {
    label: '处置预案URL',
    value: 'planUrl',
  },
  {
    label: '事件来源',
    value: 'occurSource',
  },
  {
    label: '事件发生时间',
    value: 'occurTime',
  },
  {
    label: '事件操作类型',
    value: 'eventAction',
  },
]);

const filterModule = reactive({
  form: {
    fieldName: '',
    operator: '',
    fieldValue: '',
    comName: '',
  },
  rules: {
    fieldName: {
      required: true,
      message: '请输入过滤字段',
      tirgger: 'blur',
    },
    operator: {
      required: true,
      message: '请选择过滤条件',
      tirgger: 'change',
    },
    fieldValue: {
      required: true,
      message: '请输入过滤值',
      tirgger: 'blur',
    },
  },
});
let { form, rules } = toRefs(filterModule);

const emit = defineEmits(['onClose', 'getData']);

watch(
  data,
  (val) => {
    title.value = val.comName ? val.comName : val.comType;
  },
  { deep: true, immediate: true },
);
watch(open, (val) => {
  if (val) {
    form.value = JSON.parse(JSON.stringify(data.value));
    let comName = form.value.comName ? form.value.comName : form.value.comType;
    form.value.comName = comName;
  }
  openDialog.value = val;
});
const onClose = () => {
  emit('onClose');
};
const isOk = () => {
  formRefs.value.validate((valid) => {
    if (valid) {
      form.value.comState = 1;
      emit('onClose');
      emit('getData', { ...form.value });
    }
  });
};
const fieldNameChange = (e) => {
  form.value.fieldValue = '';
  if (e == 'state') {
    showSelect.value = true;
    selectList.value = [
      {
        label: '已取消',
        value: '0',
      },
      {
        label: '处理中',
        value: '1',
      },
      {
        label: '已结案',
        value: '2',
      },
    ];
  } else if (e == 'eventAction') {
    showSelect.value = true;
    selectList.value = [
      {
        label: '代表是事件新增',
        value: '1',
      },
      {
        label: '代表是事件处置',
        value: '2',
      },
    ];
  } else {
    showSelect.value = false;
  }
};
</script>

<style lang="scss">
.directions {
  margin-top: 10px;
  line-height: 20px;
  color: red;
}
</style>
